package com.bawei.mall.product.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bawei.common.core.domain.R;
import com.bawei.mall.product.domain.*;
import com.bawei.mall.product.domain.Vo.MallProductInfoVo;
import com.bawei.mall.product.domain.Vo.MallProductSku;
import com.bawei.mall.product.mapper.*;
import com.bawei.mall.product.service.MallShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@Service
public class MallShopServiceImpl implements MallShopService {

    @Autowired
    private MallProductBrandInfoMapper mallProductBrandInfoMapper;
    @Autowired
    private MallProductInfoMapper mallProductInfoMapper;
    @Autowired
    private MallProductAttributeMapper attributeMapper;
    @Autowired
    private MallProductAttributeValuelistMapper attributeValuelistMapper;
    @Autowired
    private MallProductSkuInfoMapper skuInfoMapper;


    @Override
    public R<MallProductSku> detil(Long productId) {
        MallProductSku result = new MallProductSku();
        //异步处理
        CompletableFuture<MallProductBrandInfo> cf1 = CompletableFuture.supplyAsync(() -> {
            //品牌信息
            MallProductInfo mallProductInfo = mallProductInfoMapper.selectMallProductInfoById(productId);
            if(mallProductInfo==null){
                throw new RuntimeException("没有此商品");
            }
            String brand = mallProductInfo.getBrand();
            return  mallProductBrandInfoMapper.selectMallProductInfoById(Long.valueOf(brand));
        });

        //产品信息
        CompletableFuture<MallProductInfo> cf2= CompletableFuture.supplyAsync(()->{
            return  mallProductInfoMapper.selectMallProductInfoById(productId);
        });

        CompletableFuture<List<MallProductAttribute> > cf3= CompletableFuture.supplyAsync(()->{
            MallProductInfo mallProductInfo = mallProductInfoMapper.selectMallProductInfoById(productId);
            return attributeMapper.selectListByProductId(mallProductInfo.getRuleId());
        });

        CompletableFuture<List<MallProductAttributeValuelist>> cf4= CompletableFuture.supplyAsync(()->{
            return  attributeValuelistMapper.selectListByProductId(productId);
        });

        CompletableFuture<List<MallProductSkuInfo>> cf5= CompletableFuture.supplyAsync(()->{
            return skuInfoMapper.selectMallProductSkuInfoByProductId(productId);
        });

        try {
            MallProductBrandInfo mallProductBrandInfo = cf1.get();
            result.setBrand(mallProductBrandInfo);
            MallProductInfo mallProductInfo = cf2.get();
            result.setProduct(mallProductInfo);
            List<MallProductAttribute> mallProductAttributes = cf3.get();
            result.setProductAttributeList(mallProductAttributes);
            List<MallProductAttributeValuelist> mallProductAttributeValuelists = cf4.get();
            result.setProductAttributeValueList(mallProductAttributeValuelists);
            List<MallProductSkuInfo> sku = cf5.get();
            for (MallProductSkuInfo mallProductSkuInfo : sku) {
                String sku1 = mallProductSkuInfo.getSku();
                List<Map> list = new ArrayList<>();
                String[] split = sku1.split(",");
                for (int i = 0; i <split.length; i++) {
                    Map map = new HashMap<>();
                    map.put("key",i+1);
                    map.put("value",split[i]);
                    list.add(map);
                }
                mallProductSkuInfo.setSku(JSON.toJSONString(list));
            }

            result.setSkuStockList(sku);

            return R.ok(result);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
